package com.woniuxy.caradmin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Apply;
import com.woniuxy.caradmin.entity.pojo.User;
import com.woniuxy.caradmin.entity.pojo.Vehiclefiles;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.ApplyService;
import com.woniuxy.caradmin.service.UserService;
import com.woniuxy.caradmin.service.VehiclefilesService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author qu
 * @time 2020/12/07 17:12:11
 */
@RestController
@RequestMapping("/apply")
@Transactional

public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private VehiclefilesService vehiclefilesService;
    @Autowired
    private UserService userService;

    @GetMapping("/goauditIsInfo")
    public ModelAndView auditIsInfo(ModelAndView modelAndView) {
        modelAndView.setViewName("audit");
        return modelAndView;
    }

    @GetMapping("/goApplyadmin")
    public ModelAndView goApplyadmin(ModelAndView modelAndView) {
        modelAndView.setViewName("applyadmin");
        return modelAndView;
    }

    @GetMapping("/goApply")
    public ModelAndView goApply(ModelAndView modelAndView) {
        modelAndView.setViewName("apply");
        return modelAndView;
    }
    @ApiOperation("添加申请")
    @PostMapping("addApply")
    public void addApply(Apply apply) {
        //提交一条申请记录
        //当前用户生成一条审核记录

        Boolean result = applyService.addApply(apply);
        //管理员生成一条新的审核信息
    }
    @ApiOperation("显示所有的审批信息")
    @GetMapping("auditInfo")
    public DataGridView auditInfo() {
        //显示所有未审批信息
        Page<Apply> page = PageHelper.startPage(1, 8);
        List<Apply> applyList = applyService.selectApply();
        if (applyList != null) {
            return new DataGridView(page.getTotal(), applyList);
        } else {
            throw new CarException(ResultCode.APPLYNULL, Constaint.APPLYISNULL);
        }
    }
    @ApiOperation("显示当前员工级别剩余车辆")
    @GetMapping("auditIsInfo")
    public R auditInfo(Integer uid) {
        //显示当前的审核信息
        //Apply apply = applyService.selectIsApply(uid);
        //反映该人的员工级别
        Integer empLevel = userService.empLevel(uid);
        //反映该员工级别的剩余车辆
        List<Vehiclefiles> vehiclefilesList = vehiclefilesService.statusCarAll(empLevel);
        if (vehiclefilesList != null) {
            return R.ok().data("vehiclefilesList", vehiclefilesList);
        } else {
            throw new CarException(ResultCode.APPLYNULL, Constaint.APPLYISNULL);
        }
    }
    @ApiOperation("审核通过")
    @GetMapping("auditstatus")
    public Boolean auditstatus(Integer id) {
        //修改审核状态
        Boolean Boolean = applyService.updateApply(id);
        //修改当前车辆的状态

        return true;
    }
    @ApiOperation("审核不通过")
    @GetMapping("auditstatus0")
    public Boolean auditstatus0(Integer id) {
        //修改审核状态不通过
        Boolean Boolean = applyService.updateApply0(id);
        return true;
    }

    @GetMapping("completeApply")
    //当前审核人员的id和审核记录的id
    public R completeApply(Integer uid, Integer uid1) {
        //显示当前的审核人员姓名
        User user = userService.selectOneUser(uid1);
        //显示当前的审核信息
        Apply apply = applyService.selectIsApply(uid);
        apply.setAvailable(1);
        if (apply != null) {
            return R.ok().data("apply", apply);
        } else {
            throw new CarException(ResultCode.APPLYNULL, Constaint.APPLYISNULL);
        }
    }

    //进行派车
    @GetMapping("sendcars")
    public R sendcars(Integer uid, Integer vehiclefilesid) {
        Apply apply = applyService.selectIsApply(uid);
        Vehiclefiles vehiclefiles = vehiclefilesService.selectVehicleId(vehiclefilesid);
        vehiclefiles.setAvailable(0);

        return null;
    }
}
